<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>24.封闭对象的API操作</title>
</head>

<body>
  <script>
    // Object.seal()`方法封闭一个对象，
    // 阻止添加新属性并将所有现有属性标记为 `configurable: false,
    // 属性不能再增加/删除属性，不能修改属性的特征
    "use strict";
    const user = {
      name: "后盾人",
      age: 18
    };

    Object.seal(user);
    console.log(
      JSON.stringify(Object.getOwnPropertyDescriptors(user), null, 2)
    );

    Object.seal(user);
    //Object.isSealed 如果对象是密封的则返回 true，属性都具有 configurable: false。
    console.log(Object.isSealed(user));
    if (!Object.isSealed(user)) {
      //configurable: false。不能再添加或者删除属性了
      delete user.name;// Error 24.封闭对象的API操作.html:33 Uncaught TypeError: Cannot delete property 'name' of #<Object>

      user.name2 = '2'; //Error  24.封闭对象的API操作.html:35 Uncaught TypeError: Cannot add property name2, object is not extensible
    }
    user.name = "okk"
    console.log(user);
  </script>
</body>

</html>